﻿# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#    $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from datetime import datetime, date, time, timedelta
from odoo import api, fields, models
import base64
import csv
import StringIO

try:
    import xlwt
except ImportError:
    xlwt = None
import re
from cStringIO import StringIO

class wizard_receipt_txt(models.TransientModel):
    _name = 'wizard.file.generate'
    _description = 'Wizard that generate file.'

    generate_id = fields.Many2one('serial.generate', string=u"批量生成单", required=True)
    product_id = fields.Many2one('product.template', string=u"产品", required=True)
    total = fields.Integer(string=u"数量", related='generate_id.total', readonly=True)
    datetime = fields.Datetime(string=u"生成时间", related='generate_id.create_date', readonly=True)

    @api.multi
    @api.onchange('product_id')
    def default_generate(self):
        if not self.product_id:
            return {'domain': {'generate_id': []}}
        domain = {'generate_id': [('product_id', '=', self.product_id.id)]}
        result = {'domain': domain}
        return result

    def generate_file(self):
        #wzd_values = self.read(cr,uid,ids,context=context)[0]
        path = 'file_%s.csv' % (datetime.today().strftime("%d-%m-%Y"))
        export_objs = self.env['serial.number'].search([('generate_id', '=', self.generate_id.id)])
        
        fp = StringIO()
        with open(path, 'w') as csvfile:
            fieldnames = ['url']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
        
            for line in export_objs:
                writer.writerow({'url': str(line.trace_url)})
        
        fp.close()
        csvfile.close()
        arch = open(path, 'r').read()
        data = base64.encodestring(arch)
        attach_vals = {
                 'name': 'File%s.csv' % (datetime.today().strftime("%d-%m-%Y")),
                 'datas': data,
                 'datas_fname': 'File#_%s.csv' % (datetime.today().strftime("%d-%m-%Y")),
                 }
        doc_id = self.env['ir.attachment'].create(attach_vals)
        return {
                    'type': "ir.actions.act_url",
                    'url': "web/content/?model=ir.attachment&id="+str(doc_id.id)+"&filename_field=datas_fname&field=datas&download=true&filename="+str(doc_id.name),
                    'target': "self",
                    }


            
 #~ def from_data(self, fields, rows):
        #~ fp = StringIO()
        #~ writer = csv.writer(fp, quoting=csv.QUOTE_ALL)

        #~ writer.writerow([name.encode('utf-8') for name in fields])

        #~ for data in rows:
            #~ row = []
            #~ for d in data:
                #~ if isinstance(d, basestring):
                    #~ d = d.replace('\n',' ').replace('\t',' ')
                    #~ try:
                        #~ d = d.encode('utf-8')
                    #~ except UnicodeError:
                        #~ pass
                #~ if d is False: d = None
                #~ row.append(d)
            #~ writer.writerow(row)

        #~ fp.seek(0)
        #~ data = fp.read()
        #~ fp.close()
        #~ return data


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
